package com.xianguo.datacenter.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.xianguo.datacenter.domain.CommodityType;
import com.xianguo.datacenter.domain.User;
import com.xianguo.datacenter.mapping.CommodityTypeMapper;
import com.xianguo.datacenter.service.CommodityTypeService;
import com.xianguo.datacenter.util.StringValue;

@Service("CommTypeService")
public class CommodityTypeServiceImpl implements CommodityTypeService,StringValue {
	
	@Autowired
	private CommodityTypeMapper commmapper;
	
	@Override
	public String AddCommType(CommodityType type,User user) {
		try {
			type.setUid(user.getId());
			commmapper.addCommType(type);
			return JSONSUCCESSB;
		} catch (Exception e) {
			return JSONERRORB;
		}
	}

	@Override
	public String DelectCommType(int id,User user) {
		try {
			commmapper.deleteCommType(id,user.getId());
			return JSONSUCCESSB;
		} catch (Exception e) {
			return JSONERRORB;
		}
	}

	@Override
	public String FindCommTypeBySuper(int parmen,User user) {
		try {
			List<CommodityType> type = commmapper.findCommTypeBySuper(parmen,user.getId());
			String commtypeJSON = JSONObject.toJSONString(type);
			commtypeJSON = "{"+JSONSUCCESS+",\"data\":"+commtypeJSON+"}";
			return commtypeJSON;
		} catch (Exception e) {
			return JSONERRORB;
		}
	}

	@Override
	public String UpdateTypeName(int id,User user, String name) {
		try {
			commmapper.updateCommType(id,user.getId(),name);
			return JSONSUCCESSB;
		} catch (Exception e) {
			return JSONERRORB;
		}
	}

	@Override
	public String FindCommTypeByUid(User user) {
		try {
			List<CommodityType> commtype = commmapper.findCommTypeByid(user.getId());
			String commJSON = JSONObject.toJSONString(commtype);
			commJSON = "{"+JSONSUCCESS+",\"data\":"+commJSON+"}";
			return commJSON;
		} catch (Exception e) {
			return JSONERRORB; 
		}
	}

	@Override
	public String selectone(User user) {
		try {
			List<CommodityType> commtype = commmapper.selectone(user.getId());
			String commJSON = JSONObject.toJSONString(commtype);
			commJSON = "{"+JSONSUCCESS+",\"data\":"+commJSON+"}";
			return commJSON;
		} catch (Exception e) {
			return JSONERRORB; 
		}
	}

}
